<!-- 运行管理-设备报警记录 -->
<template>
  <div class="equip-wrap-common">
    <px-form style="padding-top: 16px" class="custom-form-ui equip-form" :model="queryParams" label-position="right" label-width="120px">
      <px-row :gutter="16">
        <px-col :span="6">
          <px-form-item label="点位名称">
            <px-input v-model="queryParams.point" placeholder="请输入点位名称进行搜索" autocomplete="off" clearable @keyup.enter="queryTable">
              <template #suffix>
                <px-icon style="cursor: pointer" @click="queryTable">
                  <search />
                </px-icon>
              </template>
            </px-input>
          </px-form-item>
        </px-col>
        <px-col :span="6">
          <px-form-item label="报警日期">
            <px-date-picker
              v-model="queryParams.taskDate"
              type="daterange"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              unlink-panels
              @change="changeDate"
            />
          </px-form-item>
        </px-col>
        <px-col :span="6">
          <px-form-item label="报警级别" prop="alarmLevel">
            <px-select v-model="queryParams.alarmLevel" placeholder="请选择报警级别" clearable filterable>
              <px-option v-for="item in filterLevelList" :key="item.id" :label="item.name" :value="item.value" />
            </px-select>
          </px-form-item>
        </px-col>
        <px-col :span="6">
          <px-button type="primary" @click="queryTable">查询</px-button>
          <px-button @click="resetTable">重置</px-button>
        </px-col>
      </px-row>
    </px-form>
    <px-table :data="tableData" class="equip-table" size="small" height="calc(100vh - 368px)" border>
      <px-table-column
        v-for="(item, index) in tableColumns"
        :key="index"
        :label="item.label"
        :min-width="item.minWidth"
        :align="item.align"
        :show-overflow-tooltip="item.showOverflowTooltip"
        :fixed="item.fixed"
      >
        <template v-if="item.type === 'level'" #default="scope">
          <px-tag style="color: #fff" round :color="tagBgColorList[scope.row.alarmLevel]"> {{ scope.row.alarmLevel }} </px-tag>
        </template>
        <template v-else-if="item.type === 'pos'" #default="scope">
          <span style="color: #3b82f6; cursor: pointer" @click="handleVisual(scope.row)">
            <px-icon><Location /></px-icon>{{ scope.row.twinName }}
          </span>
        </template>
        <template v-else-if="item.type === 'status'" #default="scope">
          <div v-if="scope.row.alarmStatus === 'OFF'">
            <px-tooltip effect="light" :content="`关闭时间：${scope.row.alarmEndTime}`">
              <px-tag type="info">已关闭</px-tag>
            </px-tooltip>
          </div>
          <px-tag v-else type="danger">报警中</px-tag>
        </template>
        <template v-else #default="scope">{{
          scope.row[item.prop] === 0 ? scope.row[item.prop] : scope.row[item.prop] ? scope.row[item.prop] : "-"
        }}</template>
      </px-table-column>
      <!-- <px-table-column fixed="right" label="操作" width="120" align="center">
        <template #default="scope">
          <px-tooltip content="详情" placement="top">
            <px-button link type="primary" icon="Tickets" @click="handleView(scope.row)" />
          </px-tooltip>
        </template>
      </px-table-column> -->
    </px-table>
    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      class="equip-table-pagination"
      :total="total"
      @pagination="queryTable"
    />
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, toRefs } from "vue";
// import { useRouter } from "vue-router";
import { queryDictData } from "@/api/common";
import { queryById } from "@/api/alert";

// const router = useRouter();
const props = defineProps(["eqpId"]);
const tagBgColorList = {
  一级报警: "#E02B2B",
  二级报警: "#FF771E",
  三级报警: "#F1B11A",
  四级报警: "#4A9BFF"
};
const state = reactive<any>({
  filterLevelList: [],
  queryParams: {
    pageNum: 1,
    pageSize: 20
  },
  tableColumns: [
    { prop: "alarmLevel", label: "级别", minWidth: "90", type: "level", showOverflowTooltip: true, fixed: "left" },
    { prop: "alarmTime", label: "发生时间", minWidth: "150", showOverflowTooltip: true },
    { prop: "point", label: "报警点位", minWidth: "330", showOverflowTooltip: true },
    { prop: "alarmContent", label: "报警内容", minWidth: "180", showOverflowTooltip: true },
    { prop: "alarmType", label: "报警类型", minWidth: "80", showOverflowTooltip: true },
    { prop: "classNameDesc", label: "科室", minWidth: "70", showOverflowTooltip: true },
    { prop: "systemName", label: "系统", minWidth: "100", showOverflowTooltip: true },
    // { prop: "position", label: "所属设备", minWidth: "130", type: "pos", showOverflowTooltip: true },
    // { prop: "twinName", label: "所属设备名称", minWidth: "120", showOverflowTooltip: true },
    // { prop: "realityValue", label: "实际值", minWidth: "100", showOverflowTooltip: true },
    // { prop: "settingValue", label: "设定值", minWidth: "100", showOverflowTooltip: true },
    { prop: "alarmAckStatus", label: "报警状态", minWidth: "80", type: "status", showOverflowTooltip: true }
    // { prop: "remark", label: "备注", minWidth: "180", type: "remark" }
  ],
  tableData: [],
  total: 0
});
const { filterLevelList, queryParams, tableColumns, tableData, total } = toRefs(state);

// 进入页面时触发
onMounted(async () => {
  filterLevelList.value = await queryDictData({ typeCode: "ALARM_LEVEL" });
  queryTable();
});

// 获取开始日期及结束日期
const changeDate = val => {
  if (val && val.length > 0) {
    queryParams.value.startTime = val[0] + " 00:00:00";
    queryParams.value.endTime = val[1] + " 23:59:59";
  } else {
    queryParams.value.startTime = "";
    queryParams.value.endTime = "";
  }
};
// 查询列表
const queryTable = async () => {
  queryParams.value.twinId = [props.eqpId];
  const params = JSON.parse(JSON.stringify(queryParams.value));
  const data = await queryById(params);
  tableData.value = data?.data || [];
  total.value = data?.total || 0;
};
// 重置筛选条件列表
const resetTable = async () => {
  queryParams.value = {
    twinId: [props.eqpId],
    pageNum: queryParams.value.pageNum,
    pageSize: queryParams.value.pageSize
  };
  queryTable();
};
// 详情
// const handleView = (row: any) => {
//   const path = "/all/alert/business";
//   router.push({
//     path: path,
//     query: { tab: row.id }
//   });
// };
</script>

<style lang="scss" scoped>
@import url("@/views/all/equip/scss/table-list.scss");

.equip-wrap-common {
  height: calc(100vh - 231px);
  padding: 0;
  overflow-x: hidden;
}
</style>
